//
//  FootmarkViewController.m
//  TravelDiary
//
//  Created by lanou3g on 15/11/11.
//  Copyright © 2015年 YDB MAC. All rights reserved.
//

#import "FootmarkViewController.h"
#import <AVOSCloud.h>
#import "ComepareTableViewCell.h"
#import "HomeModel.h"
#import <MJRefresh.h>

@interface FootmarkViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray *dataArray ;

@property(nonatomic,strong)NSMutableArray *dataArrayLD ;
@end

@implementation FootmarkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabelView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    
    self.tabelView.delegate =self ;
    
    self.tabelView.dataSource =self ;
    
    [self.tabelView registerNib:[UINib nibWithNibName:@"ComepareTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    UIImageView *backImageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
    [backImageView setImage:[UIImage imageNamed:@"tabg"]];
    self.tabelView.backgroundView=backImageView;
    
  //  self.tabelView.separatorStyle = UITableViewCellSelectionStyleNone;

    [self.view addSubview: self.tabelView];
    
    
    
    
    
    //去掉分割线
    
    
    self.dataArray = [NSMutableArray array];
    self.dataArrayLD = [NSMutableArray array];
    
    _tabelView.header = [MJRefreshNormalHeader  headerWithRefreshingBlock:^{
        
        
        AVQuery *query = [AVQuery queryWithClassName:@"ABCDE"];
        
           ;
        [query includeKey:@"FileArray"];
        
      
        
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            
            
            NSMutableArray *dataArr = [NSMutableArray array];
            for (NSDictionary * dic in objects) {
                
                HomeModel *modle = [[HomeModel alloc] init];
                [modle setValuesForKeysWithDictionary:dic[@"localData"]];
                modle.creatDate = [NSString stringWithFormat:@"%@",dic[@"createdAt"]];
                
                
                NSMutableArray *array = [NSMutableArray array];
                
                for (AVFile*file in   dic[@"FileArray"] ) {
                    
                    
                    [ array addObject:file.url];
                    
                }

                  modle.urlArray = array;
                [dataArr addObject: modle];
                
            }
            _dataArray = (NSMutableArray*)[[dataArr reverseObjectEnumerator] allObjects];
                [self.tabelView reloadData];
                
                [_tabelView.header endRefreshing];

        }];
      
    }];
    
      [_tabelView.header beginRefreshing];
   
    _tabelView.header.automaticallyChangeAlpha = YES;
  
    
    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 ;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count ;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ComepareTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[ComepareTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
        
    }
    cell.backgroundColor=[UIColor clearColor];
    cell.moeld = _dataArray[indexPath.row];
   // cell.UrlArray = _dataArrayLD;
    
    return cell ;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    HomeModel *model = self.dataArray[indexPath.row];
    
    
    return 220 + [self stringHeightWithString:model.content fontSize:18 contentSize:CGSizeMake(self.view.frame.size.width - 20, 10000)] ;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (CGFloat)stringHeightWithString:(NSString *)str fontSize:(CGFloat)fontSize contentSize:(CGSize)size
{
    
    // 第一个参数：代表最大的范围
    // 第二个参数：代表的是 是否考虑字体、是否考虑字号
    // 第三个参数：代表的是使用什么字体什么字号
    // 第四个参数：用不到，所以基本上是nil
    CGRect stringRect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil];
    
    return stringRect.size.height;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
